package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.google.zxing.Result;
import com.google.zxing.client.result.ParsedResult;
import com.mylhyl.zxing.scanner.OnScannerCompletionListener;
import com.mylhyl.zxing.scanner.ScannerView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.ItemsEntity;
import com.xilihui.xlh.business.requests.AppointmentRequest;
import com.xilihui.xlh.business.requests.JoinShopRequest;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.StatusBarUtil;
import com.xilihui.xlh.core.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;


public class ScanActivity extends BaseCompatActivity implements OnScannerCompletionListener {

    private ScannerView scannerView;
    
    int type = 1;//  我要预约(1)  立即扫码(2)

    @Override
    public int getLayout() {
        return R.layout.activity_scan;
    }

    @Override
    public String initTitle() {
        return "扫一扫";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            findViewById(R.id.space).setVisibility(View.VISIBLE);
            StatusBarUtil.setStatusBarTransparentAndLight(this);
        } else {
            findViewById(R.id.space).setVisibility(View.GONE);
        }

        setTitleColorRes(android.R.color.white);
        setHomeAsUpIndicator(R.mipmap.ic_arrow_back_white);
        
        type = getIntent().getIntExtra("type",1);

        scannerView = (ScannerView) findViewById(R.id.scannerView);
        scannerView.setLaserLineResId(R.mipmap.qrcode_default_scan_line);
        scannerView.setLaserFrameTopMargin(150);
        scannerView.setLaserFrameBoundColor(getResources().getColor(R.color.colorAccent));
        scannerView.setLaserColor(getResources().getColor(R.color.colorAccent));
        scannerView.setOnScannerCompletionListener(ScanActivity.this);


        // 闪光灯
        ImageView ivFlash = (ImageView) findViewById(R.id.ivFlash);
        ivFlash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.isSelected()) {
                    v.setSelected(false);
                    scannerView.toggleLight(false);
                } else {
                    v.setSelected(true);
                    scannerView.toggleLight(true);
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        if (scannerView != null) {
            scannerView.onResume();
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (scannerView != null) {
            scannerView.onPause();
        }
        super.onPause();
    }

    @Override
    public void OnScannerCompletion(Result rawResult, ParsedResult parsedResult, Bitmap barcode) {
        LogUtil.e(parsedResult.getDisplayResult());

        LogUtil.i("mylog","扫码二维码："+parsedResult.getDisplayResult());
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
        scannerView.restartPreviewAfterDelay(500);
        
        try {
            JSONObject jsonObject = new JSONObject(parsedResult.getDisplayResult().toString());
            int id = jsonObject.getInt("id");
            
            if(type==1){
                yuyue(id);
            }else {
                joinShop(id);
            }
            
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    
    public void yuyue(int id){
        AppointmentRequest.lijiyuyue(id)
                .compose(DoTransform.<ItemsEntity>applyScheduler(this,true))
                .compose(ScanActivity.this.<ItemsEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<ItemsEntity>(this) {
                    @Override
                    protected void onSuccess(ItemsEntity entity) {
                        Intent intent = new Intent(ScanActivity.this,GoAppointmenActivity.class);
                        intent.putExtra("data",entity);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    public void joinShop(int id){
        JoinShopRequest.joinShop(this,id+"")
                .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                .compose(ScanActivity.this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        Intent intent = new Intent(ScanActivity.this,JoinStoresActivity.class);
                        startActivity(intent);
                        finish();
                        LogUtil.i("mylog","success");
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        LogUtil.i("mylog","fail");
                    }
                });
                
    }
    
}
